﻿<%@ Page Language="C#" %>
<%@ Import Namespace="BusLib" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Route_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListItem defaultTrip = null;
        trip_num.Items.Clear();
        foreach (Trip t in BusLib.Route.GetRouteByNumber(int.Parse(Route.SelectedValue)).InServiceTrips)
        {
            bool active = false;

            if (DateTime.Today + t.StartTime <= DateTime.Now
                && DateTime.Today + t.EndTime >= DateTime.Now)
            {
                active = true;
            }
            
            string s = t.service_id + " - " + t.trip_headsign + " - " + t.StartTime.ToString() + " (" + t.trip_id + ")";
            if (active)
                s = "*" + s;

            ListItem i = new ListItem(s, t.trip_id.ToString());
            if (active && defaultTrip == null)
            {
                defaultTrip = i;
                i.Selected = true;                
            }
            trip_num.Items.Add(i);
        }
    }

    protected void trip_num_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Edit Route</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=.492&amp;sensor=false&amp;key=ABQIAAAA0AuyIOhCAnL9MOeOP1aKwRSf8xm5yYv0JxQnScVem-6GxDSR-hRhD8ZzokbFwBW-oA8z1u4gmUSiQA"></script>
    <script type="text/javascript">
        _mSvgForced = true;
        _mSvgEnabled = true;

        var map;

        function createRouteShape(poslog)
        {
            var positions = poslog.getElementsByTagName("Position");

            var points = new Array();
            for (var i = 0; i < positions.length; i++)
            {
                points.push(new GLatLng(parseFloat(GXml.value(positions[i].getElementsByTagName("lat")[0])), parseFloat(GXml.value(positions[i].getElementsByTagName("lng")[0]))));
            }

            var polyLine = new GPolyline(points, "#0000FF", 3);
            return polyLine;
        }
        
        function createStopMarker(stop) {
            var lat = parseFloat(GXml.value(stop.getElementsByTagName("lat")[0]));
            var lon = parseFloat(GXml.value(stop.getElementsByTagName("lon")[0]));

            var marker = new GMarker(new GLatLng(lat, lon));
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml("<b>" + GXml.value(stop.getElementsByTagName("stop_name")[0]) + "</b><br/>" +
                                                  "Stop # " + stop.getAttribute("id") + "<br/>" +
                                                  "Arrival time: " + GXml.value(stop.getElementsByTagName("arrival_time")[0]) + "<br/>");
            });

            return marker;
        }

        function refresh() {
            if (document.getElementById("Route").SelectedIndex == -1)
                return;

            var url = "RouteXml.aspx?route=" + document.getElementById("Route").value;
            if (document.getElementById("trip_num").SelectedIndex != -1)
                url = url + "&trip=" + document.getElementById("trip_num").value;

            GDownloadUrl(url, function(data, responseCode) {
                var xml = GXml.parse(data);
                var trip = xml.documentElement.getElementsByTagName("trip")[0];
                var shape = trip.getElementsByTagName("shape")[0];
                var points = shape.getElementsByTagName("point");
                var stoplist = trip.getElementsByTagName("stops")[0];
                var stops = stoplist.getElementsByTagName("stop");

                document.getElementById("trip_num").value = trip.getAttribute("id");
                document.getElementById("StartTime").value = GXml.value(trip.getElementsByTagName("start_time")[0]);
                document.getElementById("TripLength").value = GXml.value(trip.getElementsByTagName("length")[0]);
                document.getElementById("TripTime").value = GXml.value(trip.getElementsByTagName("duration")[0]);
                document.getElementById("StopCount").value = stops.length;

                map.clearOverlays();

                var gpoints = new Array();
                for (var i = 0; i < points.length; i++) {
                    var lat = parseFloat(GXml.value(points[i].getElementsByTagName("lat")[0]));
                    var lon = parseFloat(GXml.value(points[i].getElementsByTagName("lng")[0]));

                    gpoints.push(new GLatLng(lat, lon));
                }

                var polyLine = new GPolyline(gpoints, "#0000FF", 3);
                map.addOverlay(polyLine);
                map.panTo(polyLine.getBounds().getCenter())

                for (var i = 0; i < stops.length; i++) {
                    
                    map.addOverlay(createStopMarker(stops[i]));
                }
            })
        }

        function initialize() {
            //document.getElementById("route").value = "545";
            if (GBrowserIsCompatible()) {            
                map = new GMap2(document.getElementById("map_canvas"));
                map.addControl(new GLargeMapControl());
                map.addControl(new GMapTypeControl());
                map.setCenter(new GLatLng(47.6686609659264,-122.371513513894), 12);

                refresh();
            }
        }
    </script>
    <style type="text/css">
        .style1
        {
            width: 258px;
        }
        .style3
        {
            width: 114px;
        }
        .style4
        {
            width: 485px;
        }
    </style>
</head>
<body onload="initialize()" onunload="GUnload()" >
    <form id="form1" runat="Server">
    <table style="width:100%;">
        <tr>
            <td class="style1" rowspan="4">
                &nbsp;Route #<br />
                <asp:ListBox ID="Route" runat="server" DataSourceID="RouteListDataSource" 
                    DataTextField="route_short_name" DataValueField="route_short_name" 
                    onselectedindexchanged="Route_SelectedIndexChanged" Width="190px" 
                    Height="98px" AutoPostBack="True"></asp:ListBox>
                <asp:LinqDataSource ID="RouteListDataSource" runat="server" 
                    ContextTypeName="BusLib.BusLibDataContext" 
                    Select="new (route_short_name)" TableName="Routes">
                </asp:LinqDataSource>
                </td>
            <td class="style4" rowspan="4">
                Trip #<br />
                <asp:ListBox ID="trip_num" runat="server" 
                    onselectedindexchanged="trip_num_SelectedIndexChanged" Height="128px" 
                    Width="489px"></asp:ListBox>
            </td>
            <td class="style3">
                Start time</td>
            <td>
                <input id="StartTime" readonly="readonly" type="text" /></td>
        </tr>
        <tr>
            <td class="style3">
                Trip Length</td>
            <td>
                <input id="TripLength" readonly="readonly" type="text" /></td>
        </tr>
        <tr>
            <td class="style3">
                Trip Time</td>
            <td>
                <input id="TripTime" type="text" /></td>
        </tr>
        <tr>
            <td class="style3">
                # of Stops</td>
            <td>
                <input ID="StopCount" readonly="readonly"></input>
            </td>
        </tr>
    </table>
    <button onclick="refresh()">Refresh</button>
    <div id="map_canvas" style="width: 1000px; height: 600px"></div>
    <br />
&nbsp;</form>
</body>
</html> 